//
//  YYStatusViewModel.swift
//  YYWeibo
//
//  Created by strongwallyi on 16/1/3.
//  Copyright © 2016年 strongwallyi. All rights reserved.
//  cell 对应的视图的模型

import UIKit

class YYStatusViewModel: NSObject {

    //当前视图模型对应的模型数据
    var status: YYStatus? {
        //设置数据
        didSet{
            if let mbrank = status?.user?.mbrank where mbrank > 0&&mbrank < 7{
                memberImage = UIImage(named: "common_icon_membership_level\(mbrank)")
            }else{
                memberImage = nil
            }
            //设置认证图标
            verifiedImage = calcVerifiedImage()
            //转发评论赞显示的字符串
            reweetsCount = calcStatusCount(status?.reposts_count ?? 0, defaultTitle: "转发")
            commentsCount = calcStatusCount(status?.comments_count ?? 0, defaultTitle: "评论")
            attitudesCount = calcStatusCount(status?.attitudes_count ?? 0, defaultTitle: "赞")
            
            //判断是否有转发微博
            if let retweetText = status?.retweeted_status?.text{
                if let name = status?.retweeted_status?.user?.screen_name{
                    retweetStatusText = "@\(name):\(retweetText)"
                }
            }
            
            //判断原创微博是否有图片
            if let urls = status?.pic_urls where urls.count > 0{
                picUrls = urls
            }else if let urls = status?.retweeted_status?.pic_urls where urls.count > 0{//判断转发微博是否有图片
                picUrls = urls
            }
            
            //处理微博来源字符串
            dealSourceText()
            

        }
    }
    
    //会员显示的图片
    var memberImage: UIImage?
    //认证图片
    var verifiedImage: UIImage?
    
    //转发评论赞的字符串
    var reweetsCount: String?
    var commentsCount: String?
    var attitudesCount: String?
    
    //转发微博的内容(有转发微博的话,这个属性才有意义)
    var retweetStatusText: String?
    
    //当前微博的配图数组(可能为原创微博,也可能为转发微博)
    var picUrls: [YYStatusPictureInfo]?
    
    //格式化好的来源字符串
    var sourceText: String?
    
    //微博的创建时间
    var createText:String? {
        return NSDate.sinaDate(status?.created_at)?.sinaDescription
    }
    

    
    //处理微博来源字符串
    private func dealSourceText(){
        guard let text = status?.source else {
            return
        }

        if let preRange = text.rangeOfString("\">") {
            if let sufRange = text.rangeOfString("</") {
                sourceText = "来自\(text.substringWithRange(preRange.endIndex..<sufRange.startIndex))"
            }
        }
    }
    
    //计算转发评论的字符串
    private func calcStatusCount(count: Int, defaultTitle: String) -> String{
        
        if count == 0{
            return defaultTitle
        }
        
        if count < 10000{
            return "\(count)"
        }else{
            let result = CGFloat(count / 1000) / 10
            let resultStr = "\(result)万"
            if resultStr.containsString(".0"){
                return resultStr.stringByReplacingOccurrencesOfString(".0", withString: "")
            }
            return resultStr
        }
    }
    
    //计算认证的图片
    private func calcVerifiedImage() -> UIImage? {
        //计算图片
        if let verifiedType = status?.user?.verified_type{
            switch verifiedType{
            case -1:
                return nil
            case 0:
                return UIImage(named: "avatar_vip")
            case 2,3,5:
                return UIImage(named: "avatar_enterprise_vip")
            case 220:
                return UIImage(named: "avatar_grassroot")
            case 200:
                return UIImage(named: "avatar_vgirl")
            default:
                return nil
            }
        }
        return nil
    }
    
    override var description: String{
        let keys = ["status"]
        return dictionaryWithValuesForKeys(keys).description
    }
    
}
